package com.nf.health.app.activity.home.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nf.health.app.R;
import com.nf.health.app.activity.home.DoctorDetailsActivity;
import com.nf.health.app.activity.home.adapter.DoctorDetailsAdapter;
import com.nf.health.app.core.MyGsonBuilder;
import com.nf.health.app.customview.DividerListItemDecoration;
import com.nf.health.app.fragment.NoIBaseFragment;
import com.nf.health.app.http.Act;
import com.nf.health.app.models.DoctorCommnetVo;
import com.nf.health.app.utils.JsonUtils;

import java.util.List;

/**
 * Created by changpeng on 2017/4/8 0008.
 */

public class DoctorDetailsFragment extends NoIBaseFragment implements DoctorDetailsActivity.OpFragment{

    private View mView;
    private RecyclerView mRecyclerView;
    private DoctorDetailsAdapter mDoctorDetailsAdapter;
    private String goodAt,content,doctorid;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mView = getLayoutInflater(R.layout.fragment_details);
        addViewFillInRoot(mView);
        initView();

    }

    private void initView() {
        mRecyclerView = (RecyclerView) mView.findViewById(R.id.rv_details);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.addItemDecoration(new DividerListItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
    }


    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if(Act.Flag.DOC_DETAIL_COMMEMNT.equals(actionName)){
            String totalCount = JsonUtils.getFiledData(object.toString(), "totalCount");
            String dataStr = JsonUtils.getFiledData(object.toString(), "data");
            String list = JsonUtils.getFiledData(dataStr, "list");
            Gson gson2 = new MyGsonBuilder().createGson();
            List<DoctorCommnetVo> doctorCommnetVoList = gson2.fromJson(list, new TypeToken<List<DoctorCommnetVo>>() {
            }.getType());


            mDoctorDetailsAdapter = new DoctorDetailsAdapter(getActivity()
                     ,goodAt,content,doctorid,totalCount,doctorCommnetVoList);
            mRecyclerView.setAdapter(mDoctorDetailsAdapter);
        }
    }

    @Override
    public void handleActionError(String actionName, String response, VolleyError error) {
        super.handleActionError(actionName, response, error);
    }

    @Override
    public void onAction(String goodAt, String content, String doctorid) {
        this.goodAt = goodAt;
        this.content = content;
        this.doctorid = doctorid;
        provider.requestDoctorDetailComment2("1", "3", doctorid, Act.Flag.DOC_DETAIL_COMMEMNT);//请求医生评论列表
    }
}
